//
//  TuisuanInfoViewController.m
//  LiuHeBao
//
//  Created by zr on 2018/8/24.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "TuisuanInfoViewController.h"

@interface TuisuanInfoViewController ()

@property (nonatomic, weak)UILabel * titleLabel;
//@property (nonatomic, weak)UITextView * textView;
@property(nonatomic,weak)UIWebView *webView;
@property (nonatomic, weak)UIButton * collectButton;

@end

@implementation TuisuanInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = WHITE_COLOR;
    
    [self setupNav];
    [self setupUI];
}


- (void)setupNav{
    
    self.title = self.titleStr;
    
    UIButton * backButton = [[UIButton alloc] init];
    
    backButton.frame = CGRectMake(0, 0, 12 * SCREENH6, 20 * SCREENH6);
    [backButton setImage:[UIImage imageNamed:@"1返回"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backToView) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    self.navigationItem.leftBarButtonItem = leftButton;
    
}


- (void)setupUI{
    
    UILabel * titleLabel = [UILabel new];
    
    titleLabel.textColor = MAINCOLOR;
    titleLabel.font = [UIFont systemFontOfSize:20 * SCREENH6];
    titleLabel.text = @"/ 分析结果 /";
    
    self.titleLabel = titleLabel;
    
    [self.view addSubview:self.titleLabel];
    
    
    UIButton * collectButton = [UIButton new];
    
    [collectButton setImage:ImageNamed(@"shoucan") forState:UIControlStateNormal];
    [collectButton setImage:ImageNamed(@"shoucan2") forState:UIControlStateSelected];
    [collectButton addTarget:self action:@selector(clickCollectionButton) forControlEvents:UIControlEventTouchUpInside];
    if ([self.from isEqualToString:@"myself"]) {
        
        collectButton.selected = YES;
    }else{
        
        collectButton.selected = NO;
    }
    
    self.collectButton = collectButton;
    
    [self.view addSubview:self.collectButton];
    
//    UITextView *textView =[[UITextView alloc] init];
//    textView.backgroundColor= WHITE_COLOR;
//    textView.text = self.content;
//    textView.textColor = [UIColor blackColor];
//    textView.editable = NO;
//    textView.font = [UIFont systemFontOfSize:15 * SCREENH6];
//
//    self.textView = textView;
//
//    [self.view addSubview:self.textView];
    
    UIWebView *webView = [[UIWebView alloc] init];
    webView.scalesPageToFit = YES;
    //    webView.delegate = self;
    webView.backgroundColor = WHITE_COLOR;
    NSURL *urlPath = [NSURL fileURLWithPath:[NSBundle mainBundle].bundlePath];
    
    [webView loadHTMLString:self.content baseURL:urlPath];
    
    self.webView = webView;
    
    [self.view addSubview:self.webView];
}


- (void)viewDidLayoutSubviews{
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.view).offset(10 * SCREENH6);
    }];
    
    [self.collectButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.titleLabel);
        make.trailing.mas_equalTo(self.view).offset(-10 * SCREENH6);
        make.width.height.mas_equalTo(25 * SCREENH6);
    }];
    
//    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.leading.mas_equalTo(self.view).offset(15 * SCREENH6);
//        make.trailing.mas_equalTo(self.view).offset(-15 * SCREENH6);
//        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(10 * SCREENH6);
//        make.bottom.mas_equalTo(self.view);
//    }];
    
    [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.view).offset(15 * SCREENH6);
        make.trailing.mas_equalTo(self.view).offset(-15 * SCREENH6);
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(10 * SCREENH6);
        make.bottom.mas_equalTo(self.view);
    }];
}


- (void)clickCollectionButton{
    
    if (!self.collectButton.selected) {
        
        NSString * token = DEF_GET_OBJECT(@"token");
        NSDictionary * dict = @{@"token":token,@"pid":self.ID};
        
        [[NetworkTool sharedTools] POST:@"/index/Liuhetreasure/addCollection" parameters:dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            NSString * codeString = [NSString stringWithFormat:@"%@",responseObject[@"status"]];
            
            if ([codeString isEqualToString:@"200"]) {
                
                self.collectButton.selected = YES;
                
            }else{
                
                UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                               message:responseObject[@"msg"]
                                                                        preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                      handler:^(UIAlertAction * action) {
                                                                          
                                                                      }];
                
                [alert addAction:defaultAction];
                [self presentViewController:alert animated:YES completion:nil];
                
            }
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
        }];
    }else{
        
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                       message:@"请勿重复收藏！"
                                                                preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                              handler:^(UIAlertAction * action) {
                                                                  
                                                              }];
        
        [alert addAction:defaultAction];
        [self presentViewController:alert animated:YES completion:nil];
    }
    
    
}

- (void)backToView {
    
    [self.navigationController popViewControllerAnimated:YES];
}



@end
